package chaw.android.train;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import chaw.android.util.BoardItem;
import chaw.android.util.HttpRequestManager;
import chaw.android.util.LoadBitMap;
import chaw.android.util.Preferences;
import chaw.android.util.ServerURL;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ModifyPageListActivity extends ListActivity{
	private ArrayList<JSONObject> json_list;
	private ArrayList<BoardItem> list;
	private BoardAdapter nadapter;
	private JSONArray json_arr;
	private ImageButton ok;
	   
	 
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_page_list);
	    list = new ArrayList<BoardItem>();
	    json_list = new ArrayList<JSONObject>(); 
		ok = (ImageButton)findViewById(R.id.btnOk);

		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		}); 
		setFavoritePage();
	}
	private void setFavoritePage(){
		Intent intent = getIntent();
		String result = intent.getExtras().get("modify_list").toString();
		DownloadAsynkTask2 task = new DownloadAsynkTask2();
		task.execute(new String[]{result});

	}
	private class DownloadAsynkTask2 extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			String response = null;
			JSONObject json;
			try {
				json = new JSONObject(urls[0]);
				json_arr = new JSONArray(json.get("boards").toString());
				for(int i = 0 ; i < json_arr.length(); i++){
					JSONObject json_obj = (JSONObject) json_arr.get(i);
					json_list.add(json_obj);
				}
				for(int i = 0 ; i < json_list.size() ; i++){
					JSONObject json_obj = json_list.get(i);
					JSONObject get = (JSONObject) json_obj.get("theme");
					Bitmap image_url = null;
					if(json_obj.get("image_main").toString().equals("0")){
						image_url = null;
					}
					else if(json_obj.get("image_main").toString().equals("1")){
						image_url = LoadBitMap.fromURL(json_obj.get("image_url_1").toString());
					}
					else if(json_obj.get("image_main").toString().equals("2")){
						image_url = LoadBitMap.fromURL(json_obj.get("image_url_2").toString());
					}
					else if(json_obj.get("image_main").toString().equals("3")){
						image_url = LoadBitMap.fromURL(json_obj.get("image_url_3").toString());
					}
					else if(json_obj.get("image_main").toString().equals("4")){
						image_url = LoadBitMap.fromURL(json_obj.get("image_url_4").toString());
					}
					else{
						image_url = null;
					}
					BoardItem input = new BoardItem((String)json_obj.get("board_id"), (String)json_obj.get("title"), (String)get.get("meet"), (String)get.get("restaurant"), (String)get.get("place"), (String)get.get("lodge"), (String)get.get("etc"),image_url,(String)json_obj.get("date"),"", (String)json_obj.get("reply_count"));
					list.add(input);

				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			nadapter = new BoardAdapter(getBaseContext(), R.layout.modify_page_row, R.id.board_title, list);
		    ListView lv = getListView();
		    lv.setAdapter(nadapter);
		}
	}




	private class UploadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
		
		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
		}
	}

	private class BoardAdapter extends ArrayAdapter<BoardItem>{
		private ArrayList<BoardItem> items;
		private int rsrc;
		
		public BoardAdapter(Context ctx, int rsrcId, int txtId, ArrayList<BoardItem> data){
			super(ctx, rsrcId, txtId, data);
			this.items = data;
			this.rsrc = rsrcId;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent){
			View v = convertView;
			if(v == null){
				LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(rsrc, null);
			}
			final BoardItem e = items.get(position);

			if(e != null){
				String title = e.getTitle();
				if(title.length() > 14){
					title = title.substring(0, 13) + "..";
				}
				((TextView)v.findViewById(R.id.board_title)).setText(title);
				
				ImageView image = ((ImageView)v.findViewById(R.id.user_pic));
				if(e.getImage() == null ){
					image.setImageResource(R.drawable.icon_nophoto_b);
				}
				else{
					image.setImageBitmap(e.getImage());
				}


				ImageView i = (ImageView)v.findViewById(R.id.theme1);
				if(e.getMeet().equals("1")){
					i.setImageResource(R.drawable.icon_theme_meet);
					i = nextIcon(v,i);
				}
				if(e.getRestaurant().equals("1")){
					i.setImageResource(R.drawable.icon_theme_food);
					i = nextIcon(v,i);
				}
				if(e.getPlace().equals("1")){
					i.setImageResource(R.drawable.icon_theme_tour);
					i = nextIcon(v,i);
				}
				if(e.getLodge().equals("1")){
					i.setImageResource(R.drawable.icon_theme_lodge);
					i = nextIcon(v,i);
				}
				if(e.getEtc().equals("1")){
					i.setImageResource(R.drawable.icon_theme_etc);
					i = nextIcon(v,i);
				}
				v = iconReset(v, i);
				((ImageButton)v.findViewById(R.id.btnDelete)).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						list.remove(position);
						UploadAsynkTask task = new UploadAsynkTask();
						String[] params = new String[]{ ServerURL.deleteFavoriteBoard, "board_id", e.getId(),
									"user_id", Preferences.getValue(getBaseContext(), "user_id")};
						task.execute(params);
						nadapter.notifyDataSetChanged();
					}
				});
			}
			return v;
		}
		public ImageView nextIcon(View v, ImageView i){
			if(i.equals(v.findViewById(R.id.theme1))){
				i = (ImageView)v.findViewById(R.id.theme2);
			}
			else if(i.equals(v.findViewById(R.id.theme2))){
				i = (ImageView)v.findViewById(R.id.theme3);
			}
			else if(i.equals(v.findViewById(R.id.theme3))){
				i = (ImageView)v.findViewById(R.id.theme4);
			}
			else if(i.equals(v.findViewById(R.id.theme4))){
				i = (ImageView)v.findViewById(R.id.theme5);
			}
			return i;
		}
		public View iconReset(View v, ImageView i){
			if(i.equals(v.findViewById(R.id.theme1))){
				((ImageView)v.findViewById(R.id.theme1)).setImageDrawable(null);
				i = (ImageView)v.findViewById(R.id.theme2);
			}
			if(i.equals(v.findViewById(R.id.theme2))){
				((ImageView)v.findViewById(R.id.theme2)).setImageDrawable(null);
				i = (ImageView)v.findViewById(R.id.theme3);
			}
			if(i.equals(v.findViewById(R.id.theme3))){
				((ImageView)v.findViewById(R.id.theme3)).setImageDrawable(null);
				i = (ImageView)v.findViewById(R.id.theme4);
			}
			if(i.equals(v.findViewById(R.id.theme4))){
				((ImageView)v.findViewById(R.id.theme4)).setImageDrawable(null);
				i = (ImageView)v.findViewById(R.id.theme5);
			}
			if(i.equals(v.findViewById(R.id.theme5))){
				((ImageView)v.findViewById(R.id.theme5)).setImageDrawable(null);
			}
			return v;
		}
	}
}
	